package com.black.web.controller.system;

import com.black.common.annotation.Log;
import com.black.common.core.controller.BaseController;
import com.black.common.core.domain.R;
import com.black.common.enums.BusinessType;
import com.black.system.domain.req.system.add.SysDeptAddReq;
import com.black.system.domain.req.system.edit.SysDeptEditReq;
import com.black.system.domain.req.system.query.SysDeptQueryReq;
import com.black.system.domain.resp.system.SysDeptEditQueryResp;
import com.black.system.domain.resp.system.SysDeptResp;
import com.black.system.service.SysDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Api(tags = "后台-部门管理api")
@RestController
@RequestMapping(value = "/admin/dept")
public class SysDeptController extends BaseController {

    @Autowired
    private SysDeptService sysDeptService;

    @ApiOperation(value = "查询部门列表")
    @PreAuthorize("@ss.hasPermi('system:dept:list')")
    @GetMapping(value = "/list")
    public R<List<SysDeptResp>> list(SysDeptQueryReq req) {
        List<SysDeptResp> resp = this.sysDeptService.search(req);
        return R.ok(resp);
    }

    @ApiOperation(value = "排除指定ID及子节点的树型列表")
    @GetMapping(value = "/list/exclude/{id}")
    @PreAuthorize("@ss.hasPermi('system:dept:list')")
    public R<List<SysDeptResp>> excludeList(@PathVariable(value = "id") Long id) {
        SysDeptQueryReq req = new SysDeptQueryReq();
        req.setExcludeId(id);

        List<SysDeptResp> resp = this.sysDeptService.excludeList(req);
        return R.ok(resp);
    }

    @Log(title = "部门管理", businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增部门")
    @PreAuthorize("@ss.hasPermi('system:dept:add')")
    @PostMapping
    public R add(@RequestBody @Valid SysDeptAddReq req) {
        boolean res = this.sysDeptService.add(req);
        return super.isSuccess(res);
    }

    @Log(title = "部门管理", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除部门")
    @PreAuthorize("@ss.hasPermi('system:dept:remove')")
    @DeleteMapping(value = "/{id}")
    public R delete(@PathVariable(value = "id") Long id) {
        boolean res = this.sysDeptService.deleteById(id);
        return super.isSuccess(res);
    }

    @ApiOperation(value = "根据id查询部门")
    @PreAuthorize("@ss.hasPermi('system:dept:query')")
    @GetMapping("/{id}")
    public R<SysDeptEditQueryResp> findById(@PathVariable(value = "id") Long id) {
        SysDeptEditQueryResp resp = this.sysDeptService.findById(id);
        return R.ok(resp);
    }

    @Log(title = "部门管理", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改部门")
    @PreAuthorize("@ss.hasPermi('system:dept:edit')")
    @PutMapping
    public R edit(@RequestBody @Valid SysDeptEditReq req) {
        boolean res = this.sysDeptService.edit(req);
        return super.isSuccess(res);
    }
}
